package com.app.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.app.mapper.InviteConfigLogMapper;
import com.app.mapper.InviteConfigMapper;
import com.app.model.IntegralRuleLog;
import com.app.model.InviteConfigLogModel;
import com.app.model.InviteConfigModel;
import com.app.service.InviteConfigLogService;
import com.app.service.InviteConfigService;
import com.app.vo.IntegralRuleLogVo;
import com.app.vo.InviteConfigLogVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.query.MPJQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class InviteConfigLogServiceImpl extends ServiceImpl<InviteConfigLogMapper, InviteConfigLogModel> implements InviteConfigLogService {

    @Autowired
    private InviteConfigLogMapper inviteConfigLogMapper;

    @Override
    public InviteConfigLogVo getInsertBasicConfigList(Integer page, Integer limit,String inviteConfigId){

        InviteConfigLogVo vo = new InviteConfigLogVo();
        Page<InviteConfigLogModel> page1 = new Page<>(page, limit);

        QueryWrapper<InviteConfigLogModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("invite_config_id", inviteConfigId);
        queryWrapper.orderByDesc("create_time");

        Page<InviteConfigLogModel> voucherRecordModelPage = inviteConfigLogMapper.selectPage(page1, queryWrapper);

        List<InviteConfigLogModel> list = voucherRecordModelPage.getRecords(); // 获取分页后的记录列表
        for (InviteConfigLogModel model : list){
            String oldJsonstr = model.getOldJsonstr();
            String newJsonstr = model.getNewJsonstr();
            JSONObject oldJSON = JSONObject.parseObject(oldJsonstr);
            JSONObject newJSON = JSONObject.parseObject(newJsonstr);
            model.setOldJson(oldJSON);
            model.setNewJson(newJSON);
        }
        vo.setList(voucherRecordModelPage.getRecords());
        vo.setCount(voucherRecordModelPage.getTotal());
        return vo;
    };

}
